﻿using System.ComponentModel;

namespace Sanjeet.FastCopy.FileCopyLogic.Design2
{
    public class AsyncWorker
    {
        private IAsyncable asyncable = null;
        private BackgroundWorker worker = null;

        /// <summary>
        /// 
        /// </summary>
        public AsyncWorker(IAsyncable asyncable)
        {
            this.asyncable = asyncable;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return worker.IsBusy; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="worker"></param>
        protected virtual void PrepareWorker(BackgroundWorker worker)
        {
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //this.asyncable.
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //this.asyncable.OnProgressChanged(sender, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //IAsyncableData data = new AsyncableData();
            //data.Input = e.Argument;
            //if (e.Argument == null)
            //    this.asyncable.StartAsync();
            //else
            //    this.asyncable.StartAsync(data);
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Start()
        {
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public virtual void Start(object[] data)
        {
            worker.RunWorkerAsync(data);
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Stop()
        {
            worker.CancelAsync();
        }
    }
}